package com.yupi.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.usercenter.mapper.ChatMessageMapper;
import com.yupi.usercenter.model.domain.ChatMessage;
import com.yupi.usercenter.model.domain.User;
import com.yupi.usercenter.service.ChatMessageService;
import com.yupi.usercenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 20654
* @description 针对表【chat_message】的数据库操作Service实现
* @createDate 2025-08-12 21:37:41
*/
@Service
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage>
    implements ChatMessageService {

    @Autowired
    private UserService userService;

    @Override
    public List<ChatMessage> getHistoryMessageList(Long targetUserId, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        Page<ChatMessage> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ChatMessage> queryWrapper = new QueryWrapper<>();
        // 查询双向聊天记录
        queryWrapper.and(wrapper -> wrapper
                // 我发送给目标用户的消息
                .and(innerWrapper -> innerWrapper
                        .eq("userId", userService.getLoginUser(request).getId())
                        .eq("relatedId", targetUserId)
                )
                // 或者目标用户发送给我的消息
                .or(innerWrapper -> innerWrapper
                        .eq("userId", targetUserId)
                        .eq("relatedId", userService.getLoginUser(request).getId())
                )
        );
        queryWrapper.orderByAsc("publishTime");
        int offset = (pageNum - 1) * pageSize;
        queryWrapper.last("limit " + offset + "," + pageSize);
        return this.list(queryWrapper);
    }

    @Override
    public ChatMessage getLatestMessage(Long targetUserId, HttpServletRequest request) {
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        Long currentUserId = loginUser.getId();

        // 构建查询条件：查询与目标用户的所有聊天记录
        QueryWrapper<ChatMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                // 我发送给目标用户的消息
                .and(innerWrapper -> innerWrapper
                        .eq("userId", currentUserId)
                        .eq("relatedId", targetUserId)
                )
                // 或者目标用户发送给我的消息
                .or(innerWrapper -> innerWrapper
                        .eq("userId", targetUserId)
                        .eq("relatedId", currentUserId)
                )
        );
        queryWrapper.eq("type", 1); // 私聊类型
        queryWrapper.orderByDesc("publishTime"); // 按时间倒序
        queryWrapper.last("limit 1"); // 只取最新一条

        // 执行查询
        List<ChatMessage> messages = this.list(queryWrapper);

        // 添加空值检查
        if (messages == null || messages.isEmpty()) {
            return null; // 或者返回一个默认的ChatMessage对象
        }

        return messages.get(0);
    }
}




